
//vararge可变参数
class MagicBox06<T : Human>(vararg item: T) {
    var available = false
    private var subject: Array<out T> = item
    fun fetch(index: Int): T? {
        return subject[index].takeIf { available }
    }

    fun <R> fetch(index: Int, subjectModFunction: (T) -> R): R? {
        return subjectModFunction(subject[index]).takeIf { available }
    }

    operator fun get(index: Int): T? = subject[index]?.takeIf { available }
}

open class Human02(val age: Int)
class Boy06(val name: String, age: Int) : Human(age)
class Man03(val name: String, age: Int) : Human(age)
class Dog06(val weight: Int)

fun main() {
    val magicBox: MagicBox06<Boy06> = MagicBox06(
        Boy06("jack", 80),
        Boy06("jacky", 10),
        Boy06("john", 26)
    )
    magicBox.available = true
    magicBox.fetch(1)?.run {
        println("you find $name")
    }
    val man03 = magicBox.fetch(2) {
        Man03(it.name, it.age.plus(15))
    }


}